package com.it.iostream.Utils;


import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/*
* @Jiang:写入文件与压缩文件
*/

public class FileUtils {

    /**
     * @写入文件
     * @传入的流与文件路径不能为空
     */
    public static void writefile(@NotNull final InputStream inputStream,
                                 @NotNull @NotEmpty final String filePath,
                                                    final  String filename,
                                                    final  String nowtime)
            throws Exception {
        //文件
        String readsomething = ReadProperties.readsomething(filename);
        File file = new File(filePath+File.separator+nowtime+File.separator+readsomething);
        if (!file.exists()) {
            file.mkdirs();
        }
        /*创建输出流，写入数据*/
        OutputStream outputStream = new FileOutputStream(file+File.separator+filename);
        byte[] buffer = new byte[1024];
        int len = 0;
        try {
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    /**
     * 防止文件过大，先进行压缩
     */
    public static void zipFiles(List<File> FileList, java.io.File zipFile) throws IOException {
        byte[] buf = new byte[1024];
        //ZipOutputStream类：完成文件或文件夹的压缩
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

        for (java.io.File samllFileList : FileList) {
            FileInputStream in = new FileInputStream(samllFileList);
            out.putNextEntry(new ZipEntry(samllFileList.getName()));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
    }

    /*
    * 下载文件
    * */
   public static void downloadFile(String path,String name,HttpServletResponse response){
       try {
           // path是指欲下载的文件的路径。
           File file = new File(path);
           // 取得文件名。
           System.out.println(name);
           // 取得文件的后缀名。
           String ext = name.substring(name.lastIndexOf(".") + 1).toUpperCase();
           // 以流的形式下载文件。
           InputStream fis = new BufferedInputStream(new FileInputStream(path));
           byte[] buffer = new byte[fis.available()];
           fis.read(buffer);
           fis.close();
           // 清空response
           response.reset();
           // 设置response的Header
           response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes()));
           response.addHeader("Content-Length", "" + file.length());
           OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
           response.setContentType("application/octet-stream");
           toClient.write(buffer);
           toClient.flush();
           toClient.close();
       } catch (IOException ex) {
           ex.printStackTrace();
       }

   }










}
